package test.java;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用
            /**
             * 第一个参数Subject：商品名称
             * 第二个参数outTradeNo：编号 - 使用支付编号，业务比较简单，也可以直接使用订单编号
             * 第三个参数totalAmount：支付的总金额
             * 第四个参数resultUrl: 用户成功支付之后，跳转的页面
             */
            AlipayTradePagePayResponse response = Payment.Page()
                    .pay("Apple iPhone11 128G", "2234567890", "5799.00", "");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response.body);
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https"; // 协议
        config.gatewayHost = "openapi.alipaydev.com/gateway.do"; // 网关地址
        config.signType = "RSA2";
        config.appId = "2021000118602789";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        // 注意这个私钥，是自己的私钥
        config.merchantPrivateKey = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCcXv4oPCK+0YdBYjPwLl7dE0IiKvbC/xHc39eYu2opZoTIrNglnY7AHCBuk/Wnhob799IEZ3T1iwAjRRZp2RG4GSMjiN1xEvzkYDI+1RmSl9U6Jq30JSTlz99Dahh/fPVMs0ZgYxuEjAKSevZ0pZjk96OQ7LL2kjHz/QVC4EhpcyArIqXxHDbUekXrZLVt5yn+UgsqGmaJdFAgHo+bV9KIHVBOqpaw2RfWS+7QmHXccdmZZChMpZHp+AsfXvvWfFL8L3eCHpznK6d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Gu42fDGKdxDpdLfE7utFX7BWElYM8C9ktorzsjqiKYNBPo5Ke458INsWIpqNKcvFO+8ECgYEAuYErjBojLfWQ3XuORz03moTi7R0Qrxi153QJQmU2xPQK4giS3NmTNojXc3pT/0tbX71rk4udcO5ZBfRbxnTBoP5O8YhlRfaKSpMyWOLS6tvhxtH8bC/vBHuTTQ0C4LbGKna0Z+C94FReI17jVrJh2tLS48tbizL/cENLXhjcip0=";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1Gn2JL0+gWPVJYnKBXSQQ+ypVfnK+Apz8RzYm/sUvYDFv/e18p45v7lg11qkCqxD/zxpLoNUoOeuQ3H2loAmqv/Onnghzr+jNdirn5Kl3gknqCUET111LVjXDFkXFwj7mRAq3wo7bmedFY6rOJQ1CFBhYW/YbufZ+WSV37/Q9z3XkmnzqYxj1BNARsfHivhFmTnq0bu4elBslt47RBfxoLd7F9jdB5CCKvyjUW/9mGDU+alHCLmTnwWuAAf2LuAXss0SoecLDvLS0AlkTyJsAZVIIikru7va6xufvY+OtrYNlT2ls/g1TXIs05mlbH9KBE5Q2G4SwIq3hM/2JYNyuwIDAQAB";

        //可设置异步通知接收服务地址（可选） 需要设置，并且是一个外网能访问的域名，当然也可以使用内网穿透
        config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";

        return config;
    }
}
